package home;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;


import com.android.volley.Response;

import java.util.ArrayList;
import java.util.List;

import base.BasePage;
import bean.TopBean;
import httptool.QLParser;
import httptool.getHttpUrl;
import httptool.takeUrlResource;
import pagerindicator.TabPageIndicator;
import shanshuitiancheng.news.R;

/**
 * Created by cch on 2016/6/14.
 */
public class SettingPage extends BasePage{
    public SettingPage(Context ct){
        super(ct);
    }
    TabPageIndicator tpic;
    private ViewPager pager;
    private NewsPagerAdapter adapter;
    private ArrayList<FunctionPage> pages = new ArrayList<>();
    private int curIndex = 0;
    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.frag_news,null);
        tpic = (TabPageIndicator) view.findViewById(R.id.indicator);
        pager = (ViewPager) view.findViewById(R.id.pager);
        getJson();
        return view;
    }
    private void initIndicator() {
        pages.clear();
        for(int i = 0;i<datas.size();i++){
            String typeurl = datas.get(i).getTypeurl();
            pages.add(new FunctionPage(ct, typeurl));
        }
        adapter = new NewsPagerAdapter(ct,pages,datas);
        pager.removeAllViews();
        pager.setAdapter(adapter);

        tpic.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {

                FunctionPage page = pages.get(arg0);

                page.initData();
                curIndex = arg0;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        pages.get(0).initData();
        tpic.setViewPager(pager);
        tpic.setCurrentItem(curIndex);
    }


    @Override
    public void initData() {
        //解析頭部數據
        initIndicator();
    }
    private ArrayList<TopBean.TypesBean> datas = new ArrayList<>();
    private void getJson() {
        takeUrlResource.getJson(ct, getHttpUrl.TopUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                TopBean data = QLParser.parse(s,TopBean.class);
                int retcode = data.getRetcode();
                if(retcode==200){
                    List<TopBean.TypesBean> types = data.getTypes();
                    datas.addAll(types);
                }
            }
        });
    }


    class NewsPagerAdapter extends PagerAdapter {
        private ArrayList<FunctionPage> pages;
        private ArrayList<TopBean.TypesBean> nameData;

        public NewsPagerAdapter(Context ct, ArrayList<FunctionPage> pages,ArrayList<TopBean.TypesBean> nameData) {
            this.pages = pages;
            this.nameData = nameData;
        }
        @Override
        public void destroyItem(View container, int position, Object object) {
            // TODO Auto-generated method stub
            if(position>=pages.size()) return;
            ((ViewPager)container).removeView(pages.get(position).getRootView());
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return nameData.get(position).getTypename();
        }
        @Override
        public Object instantiateItem(View arg0, int arg1) {
            // TODO Auto-generated method stub
            ((ViewPager)arg0).addView(pages.get(arg1).getRootView(),0);
            return pages.get(arg1).getRootView();
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return nameData.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg1 == arg0;
        }

    }

}
